{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导包\n",
    "from sklearn.datasets import load_boston\n",
    "from sklearn.datasets import load_iris\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'data': array([[6.3200e-03, 1.8000e+01, 2.3100e+00, ..., 1.5300e+01, 3.9690e+02,\n",
       "         4.9800e+00],\n",
       "        [2.7310e-02, 0.0000e+00, 7.0700e+00, ..., 1.7800e+01, 3.9690e+02,\n",
       "         9.1400e+00],\n",
       "        [2.7290e-02, 0.0000e+00, 7.0700e+00, ..., 1.7800e+01, 3.9283e+02,\n",
       "         4.0300e+00],\n",
       "        ...,\n",
       "        [6.0760e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,\n",
       "         5.6400e+00],\n",
       "        [1.0959e-01, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9345e+02,\n",
       "         6.4800e+00],\n",
       "        [4.7410e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,\n",
       "         7.8800e+00]]),\n",
       " 'target': array([24. , 21.6, 34.7, 33.4, 36.2, 28.7, 22.9, 27.1, 16.5, 18.9, 15. ,\n",
       "        18.9, 21.7, 20.4, 18.2, 19.9, 23.1, 17.5, 20.2, 18.2, 13.6, 19.6,\n",
       "        15.2, 14.5, 15.6, 13.9, 16.6, 14.8, 18.4, 21. , 12.7, 14.5, 13.2,\n",
       "        13.1, 13.5, 18.9, 20. , 21. , 24.7, 30.8, 34.9, 26.6, 25.3, 24.7,\n",
       "        21.2, 19.3, 20. , 16.6, 14.4, 19.4, 19.7, 20.5, 25. , 23.4, 18.9,\n",
       "        35.4, 24.7, 31.6, 23.3, 19.6, 18.7, 16. , 22.2, 25. , 33. , 23.5,\n",
       "        19.4, 22. , 17.4, 20.9, 24.2, 21.7, 22.8, 23.4, 24.1, 21.4, 20. ,\n",
       "        20.8, 21.2, 20.3, 28. , 23.9, 24.8, 22.9, 23.9, 26.6, 22.5, 22.2,\n",
       "        23.6, 28.7, 22.6, 22. , 22.9, 25. , 20.6, 28.4, 21.4, 38.7, 43.8,\n",
       "        33.2, 27.5, 26.5, 18.6, 19.3, 20.1, 19.5, 19.5, 20.4, 19.8, 19.4,\n",
       "        21.7, 22.8, 18.8, 18.7, 18.5, 18.3, 21.2, 19.2, 20.4, 19.3, 22. ,\n",
       "        20.3, 20.5, 17.3, 18.8, 21.4, 15.7, 16.2, 18. , 14.3, 19.2, 19.6,\n",
       "        23. , 18.4, 15.6, 18.1, 17.4, 17.1, 13.3, 17.8, 14. , 14.4, 13.4,\n",
       "        15.6, 11.8, 13.8, 15.6, 14.6, 17.8, 15.4, 21.5, 19.6, 15.3, 19.4,\n",
       "        17. , 15.6, 13.1, 41.3, 24.3, 23.3, 27. , 50. , 50. , 50. , 22.7,\n",
       "        25. , 50. , 23.8, 23.8, 22.3, 17.4, 19.1, 23.1, 23.6, 22.6, 29.4,\n",
       "        23.2, 24.6, 29.9, 37.2, 39.8, 36.2, 37.9, 32.5, 26.4, 29.6, 50. ,\n",
       "        32. , 29.8, 34.9, 37. , 30.5, 36.4, 31.1, 29.1, 50. , 33.3, 30.3,\n",
       "        34.6, 34.9, 32.9, 24.1, 42.3, 48.5, 50. , 22.6, 24.4, 22.5, 24.4,\n",
       "        20. , 21.7, 19.3, 22.4, 28.1, 23.7, 25. , 23.3, 28.7, 21.5, 23. ,\n",
       "        26.7, 21.7, 27.5, 30.1, 44.8, 50. , 37.6, 31.6, 46.7, 31.5, 24.3,\n",
       "        31.7, 41.7, 48.3, 29. , 24. , 25.1, 31.5, 23.7, 23.3, 22. , 20.1,\n",
       "        22.2, 23.7, 17.6, 18.5, 24.3, 20.5, 24.5, 26.2, 24.4, 24.8, 29.6,\n",
       "        42.8, 21.9, 20.9, 44. , 50. , 36. , 30.1, 33.8, 43.1, 48.8, 31. ,\n",
       "        36.5, 22.8, 30.7, 50. , 43.5, 20.7, 21.1, 25.2, 24.4, 35.2, 32.4,\n",
       "        32. , 33.2, 33.1, 29.1, 35.1, 45.4, 35.4, 46. , 50. , 32.2, 22. ,\n",
       "        20.1, 23.2, 22.3, 24.8, 28.5, 37.3, 27.9, 23.9, 21.7, 28.6, 27.1,\n",
       "        20.3, 22.5, 29. , 24.8, 22. , 26.4, 33.1, 36.1, 28.4, 33.4, 28.2,\n",
       "        22.8, 20.3, 16.1, 22.1, 19.4, 21.6, 23.8, 16.2, 17.8, 19.8, 23.1,\n",
       "        21. , 23.8, 23.1, 20.4, 18.5, 25. , 24.6, 23. , 22.2, 19.3, 22.6,\n",
       "        19.8, 17.1, 19.4, 22.2, 20.7, 21.1, 19.5, 18.5, 20.6, 19. , 18.7,\n",
       "        32.7, 16.5, 23.9, 31.2, 17.5, 17.2, 23.1, 24.5, 26.6, 22.9, 24.1,\n",
       "        18.6, 30.1, 18.2, 20.6, 17.8, 21.7, 22.7, 22.6, 25. , 19.9, 20.8,\n",
       "        16.8, 21.9, 27.5, 21.9, 23.1, 50. , 50. , 50. , 50. , 50. , 13.8,\n",
       "        13.8, 15. , 13.9, 13.3, 13.1, 10.2, 10.4, 10.9, 11.3, 12.3,  8.8,\n",
       "         7.2, 10.5,  7.4, 10.2, 11.5, 15.1, 23.2,  9.7, 13.8, 12.7, 13.1,\n",
       "        12.5,  8.5,  5. ,  6.3,  5.6,  7.2, 12.1,  8.3,  8.5,  5. , 11.9,\n",
       "        27.9, 17.2, 27.5, 15. , 17.2, 17.9, 16.3,  7. ,  7.2,  7.5, 10.4,\n",
       "         8.8,  8.4, 16.7, 14.2, 20.8, 13.4, 11.7,  8.3, 10.2, 10.9, 11. ,\n",
       "         9.5, 14.5, 14.1, 16.1, 14.3, 11.7, 13.4,  9.6,  8.7,  8.4, 12.8,\n",
       "        10.5, 17.1, 18.4, 15.4, 10.8, 11.8, 14.9, 12.6, 14.1, 13. , 13.4,\n",
       "        15.2, 16.1, 17.8, 14.9, 14.1, 12.7, 13.5, 14.9, 20. , 16.4, 17.7,\n",
       "        19.5, 20.2, 21.4, 19.9, 19. , 19.1, 19.1, 20.1, 19.9, 19.6, 23.2,\n",
       "        29.8, 13.8, 13.3, 16.7, 12. , 14.6, 21.4, 23. , 23.7, 25. , 21.8,\n",
       "        20.6, 21.2, 19.1, 20.6, 15.2,  7. ,  8.1, 13.6, 20.1, 21.8, 24.5,\n",
       "        23.1, 19.7, 18.3, 21.2, 17.5, 16.8, 22.4, 20.6, 23.9, 22. , 11.9]),\n",
       " 'feature_names': array(['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD',\n",
       "        'TAX', 'PTRATIO', 'B', 'LSTAT'], dtype='<U7'),\n",
       " 'DESCR': \".. _boston_dataset:\\n\\nBoston house prices dataset\\n---------------------------\\n\\n**Data Set Characteristics:**  \\n\\n    :Number of Instances: 506 \\n\\n    :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.\\n\\n    :Attribute Information (in order):\\n        - CRIM     per capita crime rate by town\\n        - ZN       proportion of residential land zoned for lots over 25,000 sq.ft.\\n        - INDUS    proportion of non-retail business acres per town\\n        - CHAS     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)\\n        - NOX      nitric oxides concentration (parts per 10 million)\\n        - RM       average number of rooms per dwelling\\n        - AGE      proportion of owner-occupied units built prior to 1940\\n        - DIS      weighted distances to five Boston employment centres\\n        - RAD      index of accessibility to radial highways\\n        - TAX      full-value property-tax rate per $10,000\\n        - PTRATIO  pupil-teacher ratio by town\\n        - B        1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town\\n        - LSTAT    % lower status of the population\\n        - MEDV     Median value of owner-occupied homes in $1000's\\n\\n    :Missing Attribute Values: None\\n\\n    :Creator: Harrison, D. and Rubinfeld, D.L.\\n\\nThis is a copy of UCI ML housing dataset.\\nhttps://archive.ics.uci.edu/ml/machine-learning-databases/housing/\\n\\n\\nThis dataset was taken from the StatLib library which is maintained at Carnegie Mellon University.\\n\\nThe Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic\\nprices and the demand for clean air', J. Environ. Economics & Management,\\nvol.5, 81-102, 1978.   Used in Belsley, Kuh & Welsch, 'Regression diagnostics\\n...', Wiley, 1980.   N.B. Various transformations are used in the table on\\npages 244-261 of the latter.\\n\\nThe Boston house-price data has been used in many machine learning papers that address regression\\nproblems.   \\n     \\n.. topic:: References\\n\\n   - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261.\\n   - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.\\n\",\n",
       " 'filename': '/opt/conda/lib/python3.7/site-packages/sklearn/datasets/data/boston_house_prices.csv'}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#导入数据\n",
    "data=load_boston()\n",
    "data#数据原始的样子"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(506, 13)\n",
      "(506,) <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "X=data['data']#这样就把特征提取出来了\n",
    "y=data['target']#这样就把目标target给提取出来了\n",
    "print(X.shape)\n",
    "print(y.shape,type(y))#13个特征  506行数据  这个是回归模型 target是一群值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(506, 1) <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "y=y.reshape(-1,1)#y这个时候还是一维的的ndarray   需要把他转换成一个列\n",
    "print(y.shape,type(y))#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据规范化\n",
    "X=(X-np.mean(X,axis=0))/np.std(X,axis=0)  #减去均值除以标准差  实际上就是一个正态分布的规范化\n",
    "n_features = X.shape[1]\n",
    "n_features#就是13个维度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 准备搭建网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(13, 16)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[-0.76436994, -0.20599354,  0.565114  ,  0.2037786 ,  0.24918021,\n",
       "         0.54132099,  0.3271421 ,  1.46612485,  1.15057177, -2.0753154 ,\n",
       "        -1.33727628, -0.36630765, -0.1528942 , -0.71745203, -1.38477107,\n",
       "         0.1276674 ],\n",
       "       [ 0.80480976, -0.392884  ,  0.14616334,  1.0595428 , -0.6975159 ,\n",
       "         0.0346551 ,  0.89136219,  1.00678914, -0.00819219, -0.97094325,\n",
       "         0.70146921, -0.62170438,  0.43043981,  0.89522344,  0.10747051,\n",
       "        -2.12269946],\n",
       "       [ 0.69305387, -0.71694502,  2.07254481, -0.70277094,  0.15986164,\n",
       "         1.51870739,  0.21216534, -0.09054641,  0.24704504, -0.11856096,\n",
       "        -0.66777837,  0.60790511,  0.01249475,  1.61246436, -1.0576321 ,\n",
       "         0.01096807],\n",
       "       [ 0.56899909,  0.79438413, -0.89237079, -0.51303941, -0.3769649 ,\n",
       "        -0.41327835, -0.4392515 ,  1.00320341,  2.74828901,  0.82666021,\n",
       "        -1.17791272,  0.44957668,  0.78179009,  0.18046694, -0.25550498,\n",
       "        -1.1515642 ],\n",
       "       [-1.26785265, -0.40542264, -0.31778121,  1.87631813, -0.99208515,\n",
       "        -0.68144868, -0.20503058,  0.33417702,  0.76287522,  0.90017936,\n",
       "        -0.9262368 ,  0.54249418,  0.47351051, -2.14109366, -0.91160705,\n",
       "         0.30240909],\n",
       "       [-0.01174865, -1.10436103, -1.43235001,  0.36591734,  0.69485966,\n",
       "        -0.41422012, -1.07173694,  1.10391607,  0.12759108,  1.61257649,\n",
       "        -1.46502869,  0.0264548 ,  0.56378092,  1.99178766, -0.5681926 ,\n",
       "        -0.87646724],\n",
       "       [-0.41090193, -1.93727266,  1.410014  , -1.63162187,  0.98647111,\n",
       "        -0.47548837,  0.26538653, -0.91996634,  0.6573583 , -0.80711551,\n",
       "        -2.14499645,  0.39257512,  0.97223568, -0.16863515, -3.29391735,\n",
       "         0.88944856],\n",
       "       [-0.6419894 , -1.83639847, -0.37444741, -0.06659353, -0.25072708,\n",
       "         0.82784263,  0.21977792, -1.31946632,  0.90287931,  0.89029639,\n",
       "         2.23583631, -0.01656114,  0.27594455, -0.61933263, -1.48379801,\n",
       "         0.07432807],\n",
       "       [ 1.44287897, -0.1138549 , -0.21081784, -0.81968296,  0.59851227,\n",
       "         2.60137112, -1.25802713,  2.15424863, -0.82975953, -0.23923342,\n",
       "         0.29106936,  0.73329346,  2.80991749, -0.78447314,  0.76753875,\n",
       "         1.13782468],\n",
       "       [-0.98384472,  0.50020666, -2.10520154,  0.08919814, -0.32848333,\n",
       "        -0.21032417,  0.15771388, -1.07726643,  0.55755946, -1.21593162,\n",
       "        -0.01320636,  1.37830411, -0.04206772, -3.04484719,  1.5301057 ,\n",
       "        -0.19591147],\n",
       "       [-0.30259209, -0.13710403, -1.21064985,  0.21068909,  2.32158822,\n",
       "         0.81511832, -0.85327667,  1.11298459, -0.00331968, -1.00514468,\n",
       "        -1.11488175,  1.82660969,  0.3826455 ,  1.2356305 , -0.11696172,\n",
       "        -0.87786398],\n",
       "       [-0.98457737,  0.69366135, -0.01175958,  0.65908205,  0.16992072,\n",
       "         0.96589081, -1.34370608, -2.03663271, -0.31066186,  0.84135181,\n",
       "        -0.52356116, -1.34702652,  0.04594182, -0.72754769, -1.46989879,\n",
       "         0.59997828],\n",
       "       [ 0.58307473, -0.55238338,  0.07922979,  0.33664557, -1.47480839,\n",
       "         0.90277098, -0.57908943,  0.4508532 ,  0.65375861,  0.69069632,\n",
       "         0.0058506 , -2.53306206,  1.25896114, -0.39407815,  0.52073563,\n",
       "         0.37660817]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n_hidden = 16#16个隐藏层\n",
    "w1 = np.random.randn(n_features, n_hidden)#初始化w1 它的维度是13x16\n",
    "print(w1.shape)\n",
    "w1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] (16,)\n"
     ]
    }
   ],
   "source": [
    "b1 = np.zeros(n_hidden)#初始化b1  16个数据\n",
    "print(b1,b1.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.20144484]\n",
      " [-0.49452865]\n",
      " [-1.03089576]\n",
      " [-1.02235187]\n",
      " [-1.17689771]\n",
      " [-0.58756877]\n",
      " [ 0.21405393]\n",
      " [ 1.90185926]\n",
      " [ 0.34651458]\n",
      " [ 1.02483113]\n",
      " [-0.76608757]\n",
      " [-0.92501267]\n",
      " [ 1.01214395]\n",
      " [-1.75911266]\n",
      " [ 0.34258382]\n",
      " [-0.04578902]] (16, 1)\n"
     ]
    }
   ],
   "source": [
    "w2 = np.random.randn(n_hidden, 1)#初始化w2\n",
    "print(w2,w2.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.] (1,)\n"
     ]
    }
   ],
   "source": [
    "b2 = np.zeros(1)\n",
    "print(b2,b2.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "# relu函数\n",
    "def Relu(x):\n",
    "    result = np.where(x<0,0,x)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置学习率\n",
    "learning_rate = 1e-5\n",
    "#损失函数\n",
    "def MSE_loss(y, y_hat):\n",
    "    return np.mean(np.square(y_hat - y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义线性层\n",
    "def Linear(X, W1, b1):\n",
    "    result = X.dot(W1) + b1# X(500,13)  W1(13,16)  b1(16)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环训练 输出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w1=[[-4.91472675e-01 -2.00794383e-01  3.44410624e-01  9.01606764e-01\n",
      "  -2.57504641e-01  7.32432323e-01 -1.12690908e+00  5.46824728e-01\n",
      "   2.08046906e+00 -4.74839581e+00 -1.88769328e+00 -2.44664940e-01\n",
      "  -5.02746036e-01  3.18866050e-02 -1.09450717e+00 -3.22132889e-01]\n",
      " [ 1.55425581e+00 -3.00579368e-01 -3.23405438e-01  5.99430394e-01\n",
      "  -5.17986105e-01 -6.76152046e-01  1.47380612e+00  2.17280741e+00\n",
      "   2.72831239e-01 -6.73368857e-01  6.01826749e-01 -3.34096174e-01\n",
      "  -2.15465100e-01  4.97730265e-01  1.22391346e+00 -4.61748396e+00]\n",
      " [ 9.08858975e-01  6.84341810e-01  1.26964820e+00 -3.31909483e-01\n",
      "  -3.72211457e-01  2.68557261e+00  8.76082684e-01  2.20704535e-01\n",
      "   2.56657603e-01 -5.45307082e-01 -7.97408143e-01  1.27699685e+00\n",
      "   7.06517429e-01  2.30466238e+00 -1.45421538e+00  3.43334829e-01]\n",
      " [ 1.45294267e+00  1.83783299e+00 -1.51032104e+00  1.63169966e-01\n",
      "   4.28845944e-01 -6.06137913e-01 -1.21243303e-01  1.39112484e-01\n",
      "   2.52950768e+00 -3.66638470e-01 -1.60943437e+00  5.65018529e-01\n",
      "   1.51102556e+00 -7.37908051e-01 -8.23322727e-01 -6.57523766e-01]\n",
      " [-2.36218927e-01 -6.44632094e-01 -5.71835261e-01  3.18620361e+00\n",
      "  -2.76959577e+00 -7.99070159e-01 -1.62819997e-02  7.59048633e-02\n",
      "   1.64333794e-02  9.56906957e-01 -1.07273920e+00  1.06982216e+00\n",
      "  -2.25387926e-01 -2.09990959e+00 -6.94792813e-01  8.29673452e-01]\n",
      " [ 4.10915120e-01 -9.40647843e-02 -3.35494530e-01 -1.29205748e-01\n",
      "  -6.22851980e-01  8.80171408e-01  5.37387472e-01  1.16300209e+00\n",
      "  -7.86992769e-01  8.23226987e-01 -1.61405122e+00  7.22161870e-01\n",
      "  -3.87359140e-01  1.77440576e+00  9.78873288e-01  4.40749318e-02]\n",
      " [-1.34626042e+00 -3.38422274e-01  8.28364092e-01 -2.49142673e-01\n",
      "   8.07904131e-01 -4.53350455e-01  4.15216268e-03 -6.34228133e-01\n",
      "   5.55655119e-02 -8.91851017e-01 -1.38271158e+00 -3.62851758e-01\n",
      "   1.07218948e+00  4.13105794e-01 -2.29959291e+00 -9.40435783e-02]\n",
      " [-6.30667137e-01 -2.75012841e+00  1.50631102e-01  2.69608076e-01\n",
      "  -7.44692856e-01  9.31251802e-01  1.75117278e+00 -1.68974449e+00\n",
      "   3.77807001e-01  4.73633547e-01  1.77078779e+00  7.18015842e-02\n",
      "  -1.67375539e-01 -1.05830307e+00 -2.27097189e+00 -2.06386069e-03]\n",
      " [ 2.21787243e+00  3.06244658e-01  7.86868754e-01 -1.07413434e+00\n",
      "   4.23701842e-01  2.22226287e+00 -3.61809476e-01  2.38373437e+00\n",
      "  -8.36636334e-01  4.94454270e-01  7.91455426e-03  6.44319774e-01\n",
      "   2.36598716e+00 -7.59525346e-01  6.99570861e-01  1.46251758e+00]\n",
      " [-1.56526987e+00 -3.99526065e-01 -2.55800789e+00  9.36834598e-01\n",
      "   1.79980058e-01 -4.22121642e-01  6.07593577e-01  2.32679516e-01\n",
      "   3.20853914e-01 -1.44465209e+00 -5.27277350e-01  1.38348421e+00\n",
      "  -1.77428900e-01 -2.36747110e+00  6.32415186e-01  9.50472771e-01]\n",
      " [-2.35499364e-01 -1.72828683e+00 -1.18158344e+00 -1.08176419e+00\n",
      "   1.77983548e+00 -2.95605557e-01 -9.49248421e-01 -2.06285192e-01\n",
      "  -8.96249583e-01 -7.90619781e-01  4.20816417e-01  9.73644190e-01\n",
      "   7.40191017e-01  8.98157479e-01  1.13262046e+00  1.21668643e-01]\n",
      " [-1.13072506e+00  2.18166344e-01  1.47955614e-01  5.01211353e-02\n",
      "   7.45281198e-01 -8.06264280e-01 -8.40345490e-01  3.76564768e-01\n",
      "  -6.30698115e-01  2.15451306e-01 -1.73969878e-01 -2.05317783e+00\n",
      "   1.54833730e+00 -7.72798751e-01 -9.99371971e-01  3.59968212e-01]\n",
      " [-3.86009517e-02 -8.09063792e-01  1.23891741e+00  3.78388035e-01\n",
      "  -1.14339933e+00  1.84520771e+00 -3.41934504e-01 -1.76708111e+00\n",
      "   1.02917840e+00 -1.06367877e-01  2.33580367e-01 -2.06094413e+00\n",
      "   5.23340436e-01 -2.37170530e-01  1.82741919e+00  8.94189277e-01]] \n",
      " w2=[[-2.84534822]\n",
      " [ 2.33438634]\n",
      " [ 0.97252763]\n",
      " [-2.53358951]\n",
      " [ 2.3329201 ]\n",
      " [-2.67043669]\n",
      " [ 1.72415296]\n",
      " [ 1.6653771 ]\n",
      " [ 1.58912212]\n",
      " [ 3.96850366]\n",
      " [-0.14720961]\n",
      " [ 1.03111716]\n",
      " [-1.50951899]\n",
      " [ 1.13713953]\n",
      " [ 1.18873696]\n",
      " [ 4.06957871]]\n",
      "loss=4.934255638581301\n"
     ]
    }
   ],
   "source": [
    "#输入特征维度13 隐藏10维 输出1维（房价）\n",
    "for t in range(5000):\n",
    "    #前向传播\n",
    "    l1=Linear(X,w1,b1)#线性\n",
    "    s1=Relu(l1)#relu\n",
    "    y_pred=Linear(s1,w2,b2)#又一次线性\n",
    "    loss=MSE_loss(y,y_pred)#计算一下loss\n",
    "    #反向传播\n",
    "    grad_y_pred=2.0*(y_pred-y)\n",
    "    grad_w2 = s1.T.dot(grad_y_pred) #对w2求偏导\n",
    "    grad_temp_relu = grad_y_pred.dot(w2.T)\n",
    "    grad_temp_relu[l1<0] = 0\n",
    "    grad_w1 = X.T.dot(grad_temp_relu)#对w1求偏导\n",
    "    \n",
    "    # 更新权重\n",
    "    w1 -= learning_rate * grad_w1\n",
    "    w2 -= learning_rate * grad_w2\n",
    "\n",
    "print('w1={} \\n w2={}\\nloss={}'.format(w1, w2,MSE_loss(y,y_pred)))    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
